﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {

    var departments = JSON.parse(document.getElementById('t').value);
    departments.unshift({
        Id: "null",
        Name: "顶级部门"
    });

    function handle(departments) {
        $.each(departments, function (index, department) {
            if (department.ParentId == "null") department.ParentId = null;
        })
    }

    var dataSource = new kendo.data.TreeListDataSource({
        schema: {
            data: function (response) {
                if (!response.Datas) {
                    $.post('/department/all').success(function (res) {
                        departments = res;
                        departments.unshift({
                            Id: "null",
                            Name: "顶级部门"
                        });
                        handle(departments);
                    });
                }
                console.log(response);
                if (!response.Datas && !response.Id) {
                    handle(response);
                }
                return response.Datas || response.Id || response;
            },
            total: function (response) {
                return response.Total;
            },
            model: {
                id: "Id",
                parentId: "ParentId",
                fields: {
                    Id: {
                        nullable: false
                    },
                    Name: {
                        validation: {
                            required: true
                        }
                    },
                    Sort: {
                        validation: {
                            required: true
                        }
                    },
                    parentId: {
                        field: "ParentId",
                        nullable: true
                    }
                }
            }
        },
        transport: {
            read: {
                url: "/department/all",
                dataType: "json",
                method: "post"
            },
            update: {
                url: "/department/Update",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            destroy: {
                url: "/department/Remove",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            create: {
                url: "/department/Create",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read") {
                    if (!options.models) {
                        if (options.ParentId && options.ParentId.ParentId) {
                            options.ParentId = options.ParentId.Id;
                        }
                    }
                    return JSON.stringify({
                        department: options.models || [options]
                    });
                }
                if (operation == "read") {
                    return {
                        page: {
                            page: options.page,
                            pages: options.pageSize
                        }
                    };
                }
            }
        }
    });

    $("#grid").kendoTreeList({
        toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }],
        dataSource: dataSource,
        height: 540,
        filterable: true,
        sortable: true,

        columns: [{
            field: "Name",
            title: "部门",
            width: '180px'
        }, {
            field: "parentId",
            title: "所属部门",
            template: function (value) {
                if (value.ParentId == 'null') return "顶级部门";
                if (value.parentId == null) return "顶级部门";
                var results = null;
                $.each(departments, function (index, department) {
                    if (department.Id == value.parentId) results = department.Name;
                });
                return results;
            },
            width: '160px',
            editor: function (container, options) {
                var copy = $.extend([], departments);
                $.each(departments, function (index, department) {
                    if (department.Id == options.model.Id) copy.splice(index, 1);
                });
                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                    .kendoComboBox({
                        placeholder: "顶级部门",
                        dataTextField: "Name",
                        dataValueField: "Id",
                        dataSource: copy
                    });
            }

        }, {
            command: [{
                name: "destroy",
                'text': '删除'
            }, {
                name: "edit",
                text: '编辑'
            }]
        }]
    });
});